package com.javaee.term.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javaee.term.dto.ContactDto;
import com.javaee.term.exception.HandException;
import com.javaee.term.model.Contact;
import com.javaee.term.model.SaleMan;
import com.javaee.term.service.ContactService;
import com.javaee.term.service.SalemanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * author 陈伯瑞
 * date 2019/12/21 0021 19:45
 * version 1.0
 **/
@Controller
@RequestMapping("/seller/saleCondition")
@Slf4j
public class SaleConditonController {
    @Autowired
    ContactService contactService;
    @Autowired
    SalemanService salemanService;
    @GetMapping("/list")
    public ModelAndView list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                             @RequestParam(value = "size", defaultValue = "10") Integer size,
                             Map<String, Object> map) {

  /*    动态sql实现实现分页
        Map<String, Object> data = new HashMap();
        data.put("currIndex", (page-1)*size);
        data.put("pageSize", size);
        List<Contact> contactsInfolist= contactService.findByPage(data);*/

        /* 插件实现分页*/
        PageHelper.startPage(page, size);
        List<Contact> list = contactService.findAll();
        PageInfo<Contact> contactsInfoPage = new PageInfo<Contact>(list);
        map.put("contactsInfoPage", contactsInfoPage);
        map.put("currentPage", page);
        map.put("size", size);
        map.put("totalpage", contactsInfoPage.getPages());
        return new ModelAndView("saleCondition/list", map);
    }
    @PostMapping("/query")
    public ModelAndView query(@RequestParam(value = "manId" ) Integer manId,
                             Map<String, Object> map) {
        List<ContactDto> contactAndCustomer = contactService.findContactAndCustomer(manId);
        SaleMan one = salemanService.findOne(manId);
        try{
           if(manId==null)  throw  new HandException(0,"查询的销售人员id为空");
            if(one==null) throw  new HandException(0,"该销售人员不存在");
            if(contactAndCustomer==null) throw  new HandException(0,"没有该销售人员的签订合同");
        }catch (HandException e){
            map.put("msg", e.getMessage());
            map.put("url", "/sell/seller/saleCondition/list");
            return new ModelAndView("common/error", map);
        }
        map.put("contactAndCustomer", contactAndCustomer);
        return new ModelAndView("saleCondition/query", map);
    }
    static Calendar now = Calendar.getInstance();
    @GetMapping("/analy")
    public ModelAndView analy(Map<String, Object> map) {
        return new ModelAndView("saleCondition/data", map);
    }
    @GetMapping("/analy/saleManMonth")
    public ModelAndView analy1(Map<String, Object> map) {
        return new ModelAndView("saleCondition/dataMonth", map);
    }
    @GetMapping("/analy/saleManYear")
    public ModelAndView analy2(Map<String, Object> map) {
        return new ModelAndView("saleCondition/dataYear", map);
    }
}
